package com.xiaoxi.mementomode.game;

import com.xiaoxi.mementomode.memento.Memento;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Author xiaoxi
 * @ProjectName design_mode
 * @PackageName com.xiaoxi.mementomode.game
 * @ClassName Gamer
 * @Description TODO
 * @Date 2023/3/27 20:30
 * @Version 1.0
 */
public class Gamer {

    private int money;              // 玩家金币
    private List<String> fruits;    // 玩家水果
    private String[] fruitsName = new String[]{
            "苹果", "葡萄", "香蕉", "橘子",
    };    // 水果名称

    public Gamer(int money) {
        this.money = money;
        fruits = new ArrayList<>();
    }

    public int getMoney() {
        return money;
    }

    public List<String> getFruits() {
        return fruits;
    }

    public void bet() {
        int dice = new Random().nextInt(6) + 1;
        if (dice == 1) {
            money += 100;
            System.out.println("玩家金币增加，当前金币：" + money);
        } else if (dice == 2) {
            money /= 2;
            System.out.println("玩家金币减半，当前金币：" + money);
        } else if (dice == 6) {
            String f = fruitsName[new Random().nextInt(4)];
            fruits.add(f);
            System.out.println("玩家获得水果，当前金币：" + money + "当前水果：" + fruits.toString());
        } else {
            money -= 10;
            System.out.println("什么都没有发生，当前金币：" + money);
        }
    }

    // 保存快照
    public Memento createMemento() {
        Memento memento = new Memento(money);
        List<String> fruits = getFruits();
        for (String fruit : fruits) {
            memento.addFruits(fruit);
        }
        return memento;
    }

    // 撤回
    public void restoreMemento(Memento memento) {
        this.money = memento.getMoney();
        this.fruits = memento.getFruits();
    }

}
